<?php 
class Common_model extends CI_Model
{
	public function __construct(){}
	
	public function checkLogin($value='')
	{
		if (empty($value['password']) || empty($value['email']) || !$this->verify_email($value['email']))
	    {
			return FALSE;
	    }
		$sql_where = array('admin_email' => $value['email'], 'admin_password' => $value['password'], 'admin_status' => 'Active');
		$query = $this->db->where($sql_where)->get($this->db->dbprefix('admin'));
			
	    // User exists, now validate credentials.
		if ($query->num_rows() == 1)
	    {	
			$data=array(
						'admin_id'=>$query->row()->admin_id,
						'admin_type'=>$query->row()->admin_type,
						'admin_fullname'=>$query->row()->admin_fullname,
						'admin_email'=>$query->row()->admin_email,
						'logedin'=>TRUE
						);
			$this->set_login_sessions($data);
			$this->insert_login_detials($query->row()->admin_id);
			
			return TRUE;
	    }
	    return FALSE;
	}
	
	//user login
	public function user_checkLogin($value){
		
		$this->db->where('user_email',$value['email']);
		$this->db->where('user_password',$value['password']);
		$this->db->where('user_status','Active');
		$query=$this->db->get('scopus_user');
		if($query->num_rows()==1){
			$query=$query->result();
			
			$data=array(
						'user_id'=>$query[0]->user_id,
						'user_fullname'=>$query[0]->user_first_name,
						'user_email'=>$query[0]->user_email,
						'user_logedin'=>TRUE
						);
						
			if($this->set_user_login_sessions($data)){
				// user ip
					if (!empty($_SERVER['HTTP_CLIENT_IP'])){
						$ip=$_SERVER['HTTP_CLIENT_IP'];
					}
					elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
						$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
					}
					else{
						$ip=$_SERVER['REMOTE_ADDR'];
					}
					
					$type='Web';
					if($this->agent->is_mobile())
					$type = 'Mobile';
					
					$lat=$long=0;
					
					$geoplugin = @unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip) );
					if (is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) 
					{
						$lat = $geoplugin['geoplugin_latitude'];
						$long = $geoplugin['geoplugin_longitude'];
					}
					$date=date('Y-m-d H:i:s');
					$log=array(
						'user_history_userid' => $query[0]->user_id,
						'user_history_last_login_time' => date('Y-m-d H:i:s'),
						'user_history_last_login_ip' => htmlspecialchars(strip_tags($ip),ENT_QUOTES),
						'user_history_latitude' =>htmlspecialchars(strip_tags($lat),ENT_QUOTES),
						'user_history_longitude' =>htmlspecialchars(strip_tags($long),ENT_QUOTES),
						'user_history_device_type' =>$type,
						'user_history_status' => 'Active'
						);
						$insert=$this->db->insert('scopus_user_history_log',$log);
						return true;
				}
			
			}
	
		else {
			return false;
		}
	}
	
	public function set_user_login_sessions($data)
	{
		 $this->session->set_userdata($data);
		 if($this->session->userdata('user_logedin'))
		 return true;
		 else return false;
	}
	
	public function verify_email($email)
	{
		if (empty($email))
		{
			return FALSE;
		}
				
		$sql_select = array('admin_email');
		$sql_whrere=array('admin_email'=>$email);
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix('admin'));		 
	    $result = $query->row();

	    if ($query->num_rows() > 0)
	    {
			return $query->row()->admin_email;
	    }		
		return FALSE;
	}

	public function insert_login_detials($member_id)
	{
		    $type='Web';
		    if($this->agent->is_mobile())
			$type = 'Mobile';
			
			$lat=$long=0;
			// user ip
			if (!empty($_SERVER['HTTP_CLIENT_IP'])){
				$ip=$_SERVER['HTTP_CLIENT_IP'];
			}
			elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
				$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
			}
			else{
				$ip=$_SERVER['REMOTE_ADDR'];
			}
			$geoplugin = @unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip) );
			if (is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) 
			{
				$lat = $geoplugin['geoplugin_latitude'];
				$long = $geoplugin['geoplugin_longitude'];
			}
			$date=date('Y-m-d H:i:s');
			$log=array(
				'admin_history_adminid' => $member_id,
				'admin_history_last_login_time' => date('Y-m-d H:i:s'),
				'admin_history_last_login_ip' => htmlspecialchars(strip_tags($ip),ENT_QUOTES),
				'admin_history_latitude' =>htmlspecialchars(strip_tags($lat),ENT_QUOTES),
				'admin_history_longitude' =>htmlspecialchars(strip_tags($long),ENT_QUOTES),
				'admin_history_device_type' =>$type,
				'admin_history_status' => 'Active'
				);
			
		    $this->db->insert($this->db->dbprefix('admin_history_log'), $log);
	}

	public function set_login_sessions($data)
	{
		 $this->session->set_userdata($data);
		 if($this->session->userdata('logedin'))
		 return true;
		 else return false;
	}

	
	
	public function insertData($tablename,$insert_data=array())
	{   
		if(count($insert_data)>0)
		{
			if(count($tablename)>0)
			{
				$this->db->insert($this->db->dbprefix($tablename),$insert_data);
				return $this->db->insert_id();
			}
		}
	}
	
	function selectData($tablename="",$fieldname="",$where=NULL,$like=NULL,$orderby=NULL,$limitstart=NULL,$limitend=NULL,$groupby=NULL)
	{
		if(count($where)>0 && $where != "")
		{
			$this->db->where($where);
		}
		if(count($like)>0 && $like != "")
		{
			$this->db->like($like);
		}
		if(count($orderby)>0 && $orderby != "")
		{
			$this->db->order_by($orderby);
		}
		if(count($fieldname)>0 && $fieldname != "")
		{
			$this->db->select($fieldname);
		}
		
		if($limitstart != "" || $limitend != ""){

			if($limitend == ""){
			$this->db->limit($limitstart);
			}else{
				$this->db->limit($limitend,$limitstart);
			}
		}
		if($groupby != ""){
			$this->db->group_by($groupby);
		}
		if(count($tablename)>0)
		{
			$var=$this->db->get($tablename);
			//echo $this->db->last_query();die();
			return $var; 
		}
	}
	
	public function contact_us(){
		$contact_details_name = htmlspecialchars(strip_tags($this->input->post('name')),ENT_QUOTES);
		$contact_details_email = htmlspecialchars(strip_tags($this->input->post('email')),ENT_QUOTES);
		$contact_details_message = htmlspecialchars(strip_tags($this->input->post('message')),ENT_QUOTES);
		
		if (!empty($_SERVER['HTTP_CLIENT_IP'])){
			$ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
			$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else{
			$ip=$_SERVER['REMOTE_ADDR'];
		}
		$data=array(
				'contact_details_name'=>$contact_details_name,
				'contact_details_email'=>$contact_details_email,
				'contact_details_message'=>$contact_details_message,
				'contact_details_created_date'=>date('Y-m-d H:i:s'),
				'contact_details_created_ip'=>htmlspecialchars(strip_tags($ip),ENT_QUOTES)
				);
		$this->db->insert('scopus_contact_details',$data);
		return true;
	}
	
	public function email_forgot(){
		$email = htmlspecialchars(strip_tags($this->input->post('email')),ENT_QUOTES);
		$this->db->where('user_email',$email);
		$this->db->where('user_status','Active');
		$query=$this->db->get('scopus_user');
		if ($query->num_rows() > 0){
			return $query->result();
	    }
		else {
			return false;
		}
	}
	
	public function reset_key_check($key){
		$this->db->where('user_reset_code',$key);
		$this->db->where('user_status','Active');
		$query=$this->db->get('scopus_user');
		if ($query->num_rows() > 0){
			return $query->result();
	    }
		else {
			return false;
		}
	}
	
	public function reset_password($user_id){
		$data['user_password'] = md5(htmlspecialchars(strip_tags($this->input->post('npassword')),ENT_QUOTES));
		$data['user_reset_code'] =  md5(uniqid());
		$this->db->where('user_id',$user_id);
		$query = $this->db->update('scopus_user',$data);
		if($query){
			return true;
		}
		else {
			return false;
		}
	}
	
	public function getAllCountry()
	{
		$select = '*';
		$where = array('country_status' => 'Active');
		$result = $this->db->select($select)->where($where)->get($this->db->dbprefix('master_country'));
		$resultData = array();
		if($result->num_rows() > 0)
		{
			$resultData = $result->result_array();
		}
		return $resultData;
	}
	
	public function getGender()
	{
		$gender = array(
					'male' => 'Male',
					'female' => 'Female',
					'other' => 'Others'
					);
		return $gender;
	}
	
	public function new_application_form(){
		$this->db->where('event_application_form_event_id',$this->session->userdata('application_award_id'));
		$this->db->where('event_application_form_status','Active');
		$this->db->order_by("event_application_form_field_id","asc");
		$query=$this->db->get('scopus_event_application_form');
		return $query->result();
	}
	
	public function fecth_application_field_id($i){
		$this->db->where('master_application_id',$i);
		$this->db->where('master_application_form_status','Active');
		$query1=$this->db->get('scopus_master_application_form');
		return $query1->result(); 
	}
	
	public function new_nomination_form(){
		$this->db->where('event_nomination_form_event_id',$this->session->userdata('application_award_id'));
		$this->db->where('event_nomination_form_status','Active');
		$this->db->order_by("event_nomination_form_field_id","asc");
		$query=$this->db->get('scopus_event_nomination_form');
		return $query->result();
	}
	
	public function fecth_nomination_field_id($i){
	$this->db->where('master_nomination_id',$i);
		$this->db->where('master_nomination_form_status','Active');
		$query1=$this->db->get('scopus_master_nomination_form');
		return $query1->result(); 
	}
}
/* End of file common_model.php */
/* Location: ./application/models/common_model.php */